package bearworker.zxl.service.impl;

import bearworker.zxl.dao.mapper.ManagerSuppInfoMapper;
import bearworker.zxl.entity.ManagerSuppInfo;
import bearworker.zxl.service.SuppService;
import bearworker.zxl.utils.CommUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 逍遥的熊 on 2017/12/17.
 */
@Service("supp")
public class SuppServiceImpl implements SuppService {

    private Logger logger = Logger.getLogger(SuppServiceImpl.class);

    @Autowired
    private ManagerSuppInfoMapper managerSuppInfoMapper;

    private Map<String, Object> resMap = new HashMap<>();

    public Map<String, Object> getAllSupp(Map<String,String> params){

        Map<String,Object> map = new HashMap<>();

        int limit = Integer.valueOf(params.get("limit"));
        int offset = Integer.valueOf(params.get("offset"));

        List<ManagerSuppInfo> list = managerSuppInfoMapper.getSuppList(offset,limit);

        int cnt = managerSuppInfoMapper.getSuppCnt();

        map.put("suppList",list);
        map.put("cnt",cnt);

        resMap = CommUtil.toResMap("0","查询成功",map);

        return resMap;
    }


    public Map<String, Object> insSuppInfo(Map<String,String> params){
        ManagerSuppInfo entity = new ManagerSuppInfo();

        entity.setSuppName(params.get("suppName"));
        entity.setSuppChargeRatio(Integer.valueOf(params.get("suppChargeRatio")));
        entity.setSuppUserWxid(params.get("suppUserWxId"));
        entity.setSuppUserName(params.get("suppUserName"));
        entity.setCreateUser(Integer.valueOf(params.get("createUser")));
        entity.setUpdUser(Integer.valueOf(params.get("updUser")));
        entity.setSuppAddress(params.get("suppAddress"));
        entity.setSuppTelephone(params.get("suppTelephone"));
        int id = managerSuppInfoMapper.insert(entity);

        resMap = CommUtil.toResMap("0","添加成功",id);

        return resMap;
    }

    public Map<String, Object> updSuppInfo(Map<String,String> params){
        ManagerSuppInfo entity = new ManagerSuppInfo();

        entity.setSuppName(params.get("suppName"));
        entity.setSuppChargeRatio(Integer.valueOf(params.get("suppChargeRatio")));
        entity.setSuppUserWxid(params.get("suppUserWxId"));
        entity.setSuppUserName(params.get("suppUserName"));
        entity.setSuppAddress(params.get("suppAddress"));
        entity.setSuppTelephone(params.get("suppTelephone"));
        entity.setUpdUser(Integer.valueOf(params.get("updUser")));
        entity.setId(Integer.valueOf(params.get("id")));

        int res = managerSuppInfoMapper.updateByPrimaryKey(entity);

        resMap = CommUtil.toResMap("0","修改成功",res);

        return resMap;
    }


    public Map<String, Object> getSuppInfo(Map<String,String> params){
        int id = Integer.valueOf(params.get("id"));

        ManagerSuppInfo entity = managerSuppInfoMapper.selectByPrimaryKey(id);

        resMap = CommUtil.toResMap("0","查询成功",entity);

        return resMap;
    }

    public Map<String, Object> delSupp(Map<String,String> params){
        String ids = params.get("ids");
        int updId = Integer.valueOf(params.get("updUser"));

        String[] allId = ids.split(",");

        for(String id : allId){
            ManagerSuppInfo entity = new ManagerSuppInfo();
            entity.setId(Integer.valueOf(id));
            entity.setState(0);
            entity.setUpdUser(updId);
            managerSuppInfoMapper.updateByPrimaryKeySelective(entity);
        }

        resMap = CommUtil.toResMap("0","删除成功","");

        return resMap;
    }

    public Map<String, Object> checkSupp(Map<String,String> params){
        String ids = params.get("ids");
        int updId = Integer.valueOf(params.get("updUser"));

        String[] allId = ids.split(",");

        for(String id : allId){
            ManagerSuppInfo entity = new ManagerSuppInfo();
            entity.setId(Integer.valueOf(id));
            entity.setState(1);
            entity.setUpdUser(updId);
            managerSuppInfoMapper.updateByPrimaryKeySelective(entity);
        }

        resMap = CommUtil.toResMap("0","审核成功","");

        return resMap;
    }


}
